﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Jixiao
{
    public class TaskItem
    {
        public int RowNumber;
        public string ProjectName;
        public string TaskName { get; set; }

        public Image FinishImg { get; set; }

        public bool IsFinish { get; set; }
        public string AssignName { get; set; } 
        public DateTime StartTime {  get; set; }
        public DateTime EndTime { get; set; }
        public string MyName { get; set; }
        public string UUID { get; set; }

        internal static TaskItem FromRow(DataRow dataRow)
        {
            TaskItem item = new TaskItem();
            item.RowNumber = int.Parse(dataRow["rowNumber"].ToString());
            item.ProjectName = dataRow["ProjectName"].ToString();
            item.TaskName = dataRow["TaskName"].ToString();
            item.FinishImg = dataRow["FinishImg"] is DBNull ? null : (Image)dataRow["FinishImg"];
            item.IsFinish = int.Parse(dataRow["IsFinish"].ToString()) == 0 ? false : true;
            item.AssignName = dataRow["AssignName"].ToString();
            item.StartTime = DateTime.Parse(dataRow["StartTime"].ToString());
            item.EndTime = dataRow["EndTime"] is DBNull ? item.StartTime : DateTime.Parse(dataRow["EndTime"].ToString());
            item.MyName = dataRow["MyName"].ToString();
            item.UUID = dataRow["uid"].ToString();

            return item;
        }

        internal ListViewItem ToListViewItem()
        {
            ListViewItem listViewItem = new ListViewItem(MyName);
            listViewItem.SubItems.Add(ProjectName);
            listViewItem.SubItems.Add(TaskName);
            listViewItem.SubItems.Add(IsFinish.ToString());
            
            listViewItem.SubItems.Add(StartTime.ToString("yyyy-MM-dd"));
            listViewItem.SubItems.Add(EndTime.ToString("yyyy-MM-dd"));
            listViewItem.SubItems.Add(AssignName);

            if (this.IsFinish)
            {
                listViewItem.BackColor= Color.LightSteelBlue;
            }

            listViewItem.Tag = this;

            return listViewItem;
        }

        public string CreateInsertSql()
        {
            if (string.IsNullOrEmpty(UUID))
            {
                UUID=Guid.NewGuid().ToString();
            }
            string sql = $"INSERT INTO TaskList (rowNumber, myName, projectName, taskName, isFinish, assignName, startTime,uid) VALUES (" +
                         $"{RowNumber}, \"{MyName}\", \"{ProjectName}\", \"{TaskName}\", {(IsFinish ? 1 : 0)}, \"{AssignName}\", \"{StartTime:yyyy-MM-dd}\"," +
                         $"\"{UUID}\")";
            return sql;
        }

        internal string BuildUpdateSql()
        {
            if (string.IsNullOrEmpty(this.UUID))
            {
                this.UUID= Guid.NewGuid().ToString();
                var str = "update TaskList set IsFinish={1}, EndTime='{2}',projectName='{3}'," +
                "taskName='{4}'," +
                "assignName='{5}'," +
                "myName='{6}'," +
                "uid='{7}' " +
                "where rowNumber={0}";
                return String.Format(str, RowNumber, IsFinish ? 1 : 0, EndTime.ToString("yyyy-MM-dd"), ProjectName, TaskName, AssignName, MyName,UUID);
            }
            else
            {
                var str = "update TaskList set IsFinish={1}, EndTime='{2}',projectName='{3}'," +
                "taskName='{4}'," +
                "assignName='{5}'," +
                "myName='{6}', " +
                "startTime='{8}' "+
                "where uid='{7}'";
                return String.Format(str, RowNumber, IsFinish ? 1 : 0, EndTime.ToString("yyyy-MM-dd"), 
                    ProjectName, TaskName, AssignName, MyName,UUID,
                    StartTime);
            }
        }

        internal bool EqualsSame(TaskItem? itemB)
        {
            if (this.ProjectName != itemB.ProjectName)
            {
                return false;
            }
            if(this.TaskName != itemB.TaskName)
            {
                return false;
            }
            if (this.RowNumber != itemB.RowNumber || this.UUID != itemB.UUID)
            {
                return false;
            }
            
            return true;
        }
    }
}
